package 多线程;

/**
 * @ClassName : DeadLockTest
 * @Author : LV88VL
 * @Date: 2021/3/7 15:37
 * @Description : 死锁测试
 */

class Test implements Runnable
{
    private boolean flag;
    Test(boolean flag)
    {
        this.flag=flag;
    }
    @Override
    public void run() {
        if (flag) {
            while (true){
                synchronized (Mylock.LOckA){
                    System.out.println("if*******************lockA");
                    synchronized (Mylock.LOckB){
                        System.out.println("else*******************lockB");
                    }
                }
            }
        }
        else {
            while(true){
                synchronized(Mylock.LOckB){
                    System.out.println("if*******************lockB");
                    synchronized (Mylock.LOckA){
                        System.out.println("else*******************lockA");
                    }
                }
            }
        }
    }
}
class Mylock{
    public static final Object LOckA=new Object();
    public static final Object LOckB=new Object();
}
public class DeadLockTest {
    public static void main(String[] args)
    {
        Test t1=new Test(true);
        Test t2=new Test(false);
        Thread t11=new Thread(t1);
        Thread t22=new Thread(t2);
        t11.start();
        t22.start();
    }
}
